package com.yumingjiang.common.template;

import com.yumingjiang.common.model.Result;
import jakarta.servlet.ServletRequest;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * @description:
 * @author: John
 * @version: ControllerTemplate.java, v1.0 2024年12月30日 03:27
 */
@Slf4j
@Component
public class ControllerTemplate {

    public <T extends ServletRequest> Result process(T t, CommonCallback callBack) {
        long startTime = System.currentTimeMillis();
        String requestURI = null;
        Result m = null;
        try {
            if (t instanceof HttpServletRequest) {
                HttpServletRequest t1 = (HttpServletRequest) t;
                requestURI = t1.getRequestURI();
            }
            m = callBack.doBiz();
        }catch (Exception e) {
            log.error("Controller调用系统异常", e);
            m = new Result<>();
            m.setSuccess(false);
            m.setCode(-1);
            m.setMessage(e.getMessage());
        } finally {
            long endTime = System.currentTimeMillis();
            log.info("CONTROLLER-LISTENER:{}|{}|{}|{}|{}", requestURI, m.isSuccess(), m.getCode(), m.getMessage(), endTime - startTime);
        }
        return m;
    }
}